using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Text;
using System.Xml;
using System.Data;
using ExceptionHandler;
using System.Configuration;
using System.Globalization;
using System.Diagnostics;
using Microsoft.ApplicationBlocks.ExceptionManagement;

namespace ASUV
{

	/// <summary>
	/// 
	/// </summary>
    public class ASAddQueryForm : ASUV.ASChildForm
	{
		#region [ members ]
		private TabControl addQueryTabControl;
        private TabPage importTabPage;
        private Button importButton;
        private Label selectFileLabel;
        private Button browseButton;
        private TextBox selectFileTextBox;
        private Button clearButton;
        private Button saveButton;
        private DataGridView ImportQueryDataGridView;
        private TabPage randomQueryTabPage;
        private Button clearGeneratedQueryButton;
		private Label numberOfQueriesLabel;
        private Button saveGeneratedQueryButton;
		private TextBox numberOfQueriesTextBox;
        private Button generateQueryButton;
        private Label cubeNameLabel;
        private TextBox cubeNameTextBox;
        private OpenFileDialog mdxQeryOpenFileDialog;
        private System.ComponentModel.IContainer components = null;
        ProjectFileFoundEventArgs projectDetails;
		private DataGridView randomQueryDataGridView;
		private Button closeButton;
		private Button close1Button;
		private Label messageLabel;
		private Label message1Label;
		private DataGridViewTextBoxColumn dataGridViewTextBoxColumn1;
		private DataGridViewTextBoxColumn dataGridViewTextBoxColumn2;
		private DataGridViewTextBoxColumn dataGridViewTextBoxColumn3;
		private DataGridViewTextBoxColumn dataGridViewTextBoxColumn4;
		private DataGridViewTextBoxColumn dataGridViewTextBoxColumn5;
		private DataGridViewTextBoxColumn dataGridViewTextBoxColumn6;
		string[] files;
		public delegate void QueriesAddedEvent();
		public event QueriesAddedEvent QueriesAdded;
		#endregion

		#region public ASAddQueryForm(ProjectFileFoundEventArgs args)
		public ASAddQueryForm(ProjectFileFoundEventArgs args)
        {
            // This call is required by the Windows Form Designer.
            projectDetails = args;
            InitializeComponent();

            // TODO: Add any initialization after the InitializeComponent call
		}
		#endregion

		#region protected override void Dispose(bool disposing)
		/// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose(disposing);
		}
		#endregion

		#region Designer generated code
		/// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
			System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle();
			System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle2 = new System.Windows.Forms.DataGridViewCellStyle();
			System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle3 = new System.Windows.Forms.DataGridViewCellStyle();
			System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle4 = new System.Windows.Forms.DataGridViewCellStyle();
			this.addQueryTabControl = new System.Windows.Forms.TabControl();
			this.importTabPage = new System.Windows.Forms.TabPage();
			this.messageLabel = new System.Windows.Forms.Label();
			this.closeButton = new System.Windows.Forms.Button();
			this.importButton = new System.Windows.Forms.Button();
			this.selectFileLabel = new System.Windows.Forms.Label();
			this.browseButton = new System.Windows.Forms.Button();
			this.selectFileTextBox = new System.Windows.Forms.TextBox();
			this.clearButton = new System.Windows.Forms.Button();
			this.saveButton = new System.Windows.Forms.Button();
			this.ImportQueryDataGridView = new System.Windows.Forms.DataGridView();
			this.dataGridViewTextBoxColumn1 = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.dataGridViewTextBoxColumn2 = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.dataGridViewTextBoxColumn3 = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.randomQueryTabPage = new System.Windows.Forms.TabPage();
			this.message1Label = new System.Windows.Forms.Label();
			this.close1Button = new System.Windows.Forms.Button();
			this.randomQueryDataGridView = new System.Windows.Forms.DataGridView();
			this.dataGridViewTextBoxColumn4 = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.dataGridViewTextBoxColumn5 = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.dataGridViewTextBoxColumn6 = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.clearGeneratedQueryButton = new System.Windows.Forms.Button();
			this.numberOfQueriesLabel = new System.Windows.Forms.Label();
			this.saveGeneratedQueryButton = new System.Windows.Forms.Button();
			this.numberOfQueriesTextBox = new System.Windows.Forms.TextBox();
			this.generateQueryButton = new System.Windows.Forms.Button();
			this.cubeNameLabel = new System.Windows.Forms.Label();
			this.cubeNameTextBox = new System.Windows.Forms.TextBox();
			this.mdxQeryOpenFileDialog = new System.Windows.Forms.OpenFileDialog();
			this.addQueryTabControl.SuspendLayout();
			this.importTabPage.SuspendLayout();
			((System.ComponentModel.ISupportInitialize)(this.ImportQueryDataGridView)).BeginInit();
			this.randomQueryTabPage.SuspendLayout();
			((System.ComponentModel.ISupportInitialize)(this.randomQueryDataGridView)).BeginInit();
			this.SuspendLayout();
			// 
			// addQueryTabControl
			// 
			this.addQueryTabControl.Controls.Add(this.importTabPage);
			this.addQueryTabControl.Controls.Add(this.randomQueryTabPage);
			this.addQueryTabControl.Location = new System.Drawing.Point(15, 12);
			this.addQueryTabControl.Name = "addQueryTabControl";
			this.addQueryTabControl.SelectedIndex = 0;
			this.addQueryTabControl.Size = new System.Drawing.Size(680, 590);
			this.addQueryTabControl.TabIndex = 1;
			// 
			// importTabPage
			// 
			this.importTabPage.Controls.Add(this.messageLabel);
			this.importTabPage.Controls.Add(this.closeButton);
			this.importTabPage.Controls.Add(this.importButton);
			this.importTabPage.Controls.Add(this.selectFileLabel);
			this.importTabPage.Controls.Add(this.browseButton);
			this.importTabPage.Controls.Add(this.selectFileTextBox);
			this.importTabPage.Controls.Add(this.clearButton);
			this.importTabPage.Controls.Add(this.saveButton);
			this.importTabPage.Controls.Add(this.ImportQueryDataGridView);
			this.importTabPage.Location = new System.Drawing.Point(4, 22);
			this.importTabPage.Name = "importTabPage";
			this.importTabPage.Padding = new System.Windows.Forms.Padding(3);
			this.importTabPage.Size = new System.Drawing.Size(672, 564);
			this.importTabPage.TabIndex = 0;
			this.importTabPage.Text = "Import MDX Query";
			// 
			// messageLabel
			// 
			this.messageLabel.AutoSize = true;
			this.messageLabel.Location = new System.Drawing.Point(8, 454);
			this.messageLabel.Name = "messageLabel";
			this.messageLabel.Size = new System.Drawing.Size(141, 13);
			this.messageLabel.TabIndex = 15;
			this.messageLabel.Text = "Note: Query Name is editable";
			this.messageLabel.Visible = false;
			// 
			// closeButton
			// 
			this.closeButton.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
			this.closeButton.Location = new System.Drawing.Point(563, 484);
			this.closeButton.Name = "closeButton";
			this.closeButton.Size = new System.Drawing.Size(95, 23);
			this.closeButton.TabIndex = 5;
			this.closeButton.Text = "Close";
			this.closeButton.Click += new System.EventHandler(this.closeButton_Click);
			// 
			// importButton
			// 
			this.importButton.Enabled = false;
			this.importButton.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
			this.importButton.Location = new System.Drawing.Point(454, 50);
			this.importButton.Name = "importButton";
			this.importButton.Size = new System.Drawing.Size(95, 23);
			this.importButton.TabIndex = 2;
			this.importButton.Text = "Import";
			this.importButton.Click += new System.EventHandler(this.importButton_Click);
			// 
			// selectFileLabel
			// 
			this.selectFileLabel.AutoSize = true;
			this.selectFileLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
			this.selectFileLabel.Location = new System.Drawing.Point(8, 53);
			this.selectFileLabel.Name = "selectFileLabel";
			this.selectFileLabel.Size = new System.Drawing.Size(63, 15);
			this.selectFileLabel.TabIndex = 12;
			this.selectFileLabel.Text = "Select File:";
			// 
			// browseButton
			// 
			this.browseButton.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
			this.browseButton.Location = new System.Drawing.Point(343, 50);
			this.browseButton.Name = "browseButton";
			this.browseButton.Size = new System.Drawing.Size(95, 23);
			this.browseButton.TabIndex = 1;
			this.browseButton.Text = "Browse";
			this.browseButton.Click += new System.EventHandler(this.browseButton_Click);
			// 
			// selectFileTextBox
			// 
			this.selectFileTextBox.BackColor = System.Drawing.SystemColors.ButtonHighlight;
			this.selectFileTextBox.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.selectFileTextBox.Location = new System.Drawing.Point(109, 53);
			this.selectFileTextBox.Name = "selectFileTextBox";
			this.selectFileTextBox.ReadOnly = true;
			this.selectFileTextBox.Size = new System.Drawing.Size(219, 20);
			this.selectFileTextBox.TabIndex = 0;
			// 
			// clearButton
			// 
			this.clearButton.Enabled = false;
			this.clearButton.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
			this.clearButton.Location = new System.Drawing.Point(454, 484);
			this.clearButton.Name = "clearButton";
			this.clearButton.Size = new System.Drawing.Size(95, 23);
			this.clearButton.TabIndex = 4;
			this.clearButton.Text = "Clear";
			this.clearButton.Click += new System.EventHandler(this.clearButton_Click);
			// 
			// saveButton
			// 
			this.saveButton.Enabled = false;
			this.saveButton.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
			this.saveButton.Location = new System.Drawing.Point(343, 484);
			this.saveButton.Name = "saveButton";
			this.saveButton.Size = new System.Drawing.Size(95, 23);
			this.saveButton.TabIndex = 3;
			this.saveButton.Text = "Save";
			this.saveButton.Click += new System.EventHandler(this.saveButton_Click);
			// 
			// ImportQueryDataGridView
			// 
			this.ImportQueryDataGridView.AllowUserToAddRows = false;
			this.ImportQueryDataGridView.AllowUserToResizeRows = false;
			this.ImportQueryDataGridView.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill;
			dataGridViewCellStyle1.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
			dataGridViewCellStyle1.BackColor = System.Drawing.SystemColors.Control;
			dataGridViewCellStyle1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
			dataGridViewCellStyle1.ForeColor = System.Drawing.SystemColors.WindowText;
			dataGridViewCellStyle1.SelectionBackColor = System.Drawing.SystemColors.Highlight;
			dataGridViewCellStyle1.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
			dataGridViewCellStyle1.WrapMode = System.Windows.Forms.DataGridViewTriState.True;
			this.ImportQueryDataGridView.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle1;
			this.ImportQueryDataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
			this.ImportQueryDataGridView.Columns.Add(this.dataGridViewTextBoxColumn1);
			this.ImportQueryDataGridView.Columns.Add(this.dataGridViewTextBoxColumn2);
			this.ImportQueryDataGridView.Columns.Add(this.dataGridViewTextBoxColumn3);
			this.ImportQueryDataGridView.EditMode = System.Windows.Forms.DataGridViewEditMode.EditOnKeystroke;
			this.ImportQueryDataGridView.Location = new System.Drawing.Point(8, 116);
			this.ImportQueryDataGridView.Name = "ImportQueryDataGridView";
			this.ImportQueryDataGridView.Size = new System.Drawing.Size(650, 332);
			this.ImportQueryDataGridView.TabIndex = 6;
			this.ImportQueryDataGridView.UserDeletedRow += new System.Windows.Forms.DataGridViewRowEventHandler(this.ImportQueryDataGridView_UserDeletedRow);
			// 
			// dataGridViewTextBoxColumn1
			// 
			dataGridViewCellStyle2.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleRight;
			dataGridViewCellStyle2.FormatProvider = new System.Globalization.CultureInfo("en-US");
			this.dataGridViewTextBoxColumn1.DefaultCellStyle = dataGridViewCellStyle2;
			this.dataGridViewTextBoxColumn1.FillWeight = 32.18396F;
			this.dataGridViewTextBoxColumn1.HeaderText = "SI No.";
			this.dataGridViewTextBoxColumn1.MinimumWidth = 20;
			this.dataGridViewTextBoxColumn1.Name = "clmn1";
			this.dataGridViewTextBoxColumn1.ReadOnly = true;
			// 
			// dataGridViewTextBoxColumn2
			// 
			this.dataGridViewTextBoxColumn2.FillWeight = 137.6466F;
			this.dataGridViewTextBoxColumn2.HeaderText = "Query Name";
			this.dataGridViewTextBoxColumn2.MinimumWidth = 70;
			this.dataGridViewTextBoxColumn2.Name = "Column2";
			// 
			// dataGridViewTextBoxColumn3
			// 
			this.dataGridViewTextBoxColumn3.FillWeight = 217.5462F;
			this.dataGridViewTextBoxColumn3.HeaderText = "MDX Query";
			this.dataGridViewTextBoxColumn3.Name = "Column1";
			this.dataGridViewTextBoxColumn3.ReadOnly = true;
			// 
			// randomQueryTabPage
			// 
			this.randomQueryTabPage.Controls.Add(this.message1Label);
			this.randomQueryTabPage.Controls.Add(this.close1Button);
			this.randomQueryTabPage.Controls.Add(this.randomQueryDataGridView);
			this.randomQueryTabPage.Controls.Add(this.clearGeneratedQueryButton);
			this.randomQueryTabPage.Controls.Add(this.numberOfQueriesLabel);
			this.randomQueryTabPage.Controls.Add(this.saveGeneratedQueryButton);
			this.randomQueryTabPage.Controls.Add(this.numberOfQueriesTextBox);
			this.randomQueryTabPage.Controls.Add(this.generateQueryButton);
			this.randomQueryTabPage.Controls.Add(this.cubeNameLabel);
			this.randomQueryTabPage.Controls.Add(this.cubeNameTextBox);
			this.randomQueryTabPage.Location = new System.Drawing.Point(4, 22);
			this.randomQueryTabPage.Name = "randomQueryTabPage";
			this.randomQueryTabPage.Padding = new System.Windows.Forms.Padding(3);
			this.randomQueryTabPage.Size = new System.Drawing.Size(672, 564);
			this.randomQueryTabPage.TabIndex = 1;
			this.randomQueryTabPage.Text = "Random Query";
			// 
			// message1Label
			// 
			this.message1Label.AutoSize = true;
			this.message1Label.Location = new System.Drawing.Point(8, 454);
			this.message1Label.Name = "message1Label";
			this.message1Label.Size = new System.Drawing.Size(141, 13);
			this.message1Label.TabIndex = 26;
			this.message1Label.Text = "Note: Query Name is editable";
			this.message1Label.Visible = false;
			// 
			// close1Button
			// 
			this.close1Button.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
			this.close1Button.Location = new System.Drawing.Point(563, 485);
			this.close1Button.Name = "close1Button";
			this.close1Button.Size = new System.Drawing.Size(95, 23);
			this.close1Button.TabIndex = 5;
			this.close1Button.Text = "Close";
			this.close1Button.Click += new System.EventHandler(this.close1Button_Click);
			// 
			// randomQueryDataGridView
			// 
			this.randomQueryDataGridView.AllowUserToAddRows = false;
			this.randomQueryDataGridView.AllowUserToOrderColumns = true;
			this.randomQueryDataGridView.AllowUserToResizeRows = false;
			this.randomQueryDataGridView.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill;
			dataGridViewCellStyle3.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
			dataGridViewCellStyle3.BackColor = System.Drawing.SystemColors.Control;
			dataGridViewCellStyle3.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
			dataGridViewCellStyle3.ForeColor = System.Drawing.SystemColors.WindowText;
			dataGridViewCellStyle3.SelectionBackColor = System.Drawing.SystemColors.Highlight;
			dataGridViewCellStyle3.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
			dataGridViewCellStyle3.WrapMode = System.Windows.Forms.DataGridViewTriState.True;
			this.randomQueryDataGridView.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle3;
			this.randomQueryDataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
			this.randomQueryDataGridView.Columns.Add(this.dataGridViewTextBoxColumn4);
			this.randomQueryDataGridView.Columns.Add(this.dataGridViewTextBoxColumn5);
			this.randomQueryDataGridView.Columns.Add(this.dataGridViewTextBoxColumn6);
			this.randomQueryDataGridView.EditMode = System.Windows.Forms.DataGridViewEditMode.EditOnKeystroke;
			this.randomQueryDataGridView.Location = new System.Drawing.Point(8, 116);
			this.randomQueryDataGridView.Name = "randomQueryDataGridView";
			this.randomQueryDataGridView.Size = new System.Drawing.Size(650, 332);
			this.randomQueryDataGridView.TabIndex = 24;
			this.randomQueryDataGridView.UserDeletedRow += new System.Windows.Forms.DataGridViewRowEventHandler(this.randomQueryDataGridView_UserDeletedRow);
			// 
			// dataGridViewTextBoxColumn4
			// 
			dataGridViewCellStyle4.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleRight;
			dataGridViewCellStyle4.FormatProvider = new System.Globalization.CultureInfo("en-US");
			this.dataGridViewTextBoxColumn4.DefaultCellStyle = dataGridViewCellStyle4;
			this.dataGridViewTextBoxColumn4.FillWeight = 49.15949F;
			this.dataGridViewTextBoxColumn4.HeaderText = "SI No.";
			this.dataGridViewTextBoxColumn4.MinimumWidth = 25;
			this.dataGridViewTextBoxColumn4.Name = "clmn1";
			this.dataGridViewTextBoxColumn4.ReadOnly = true;
			// 
			// dataGridViewTextBoxColumn5
			// 
			this.dataGridViewTextBoxColumn5.FillWeight = 93.26638F;
			this.dataGridViewTextBoxColumn5.HeaderText = "Query Name";
			this.dataGridViewTextBoxColumn5.MinimumWidth = 70;
			this.dataGridViewTextBoxColumn5.Name = "Column2";
			// 
			// dataGridViewTextBoxColumn6
			// 
			this.dataGridViewTextBoxColumn6.FillWeight = 244.9509F;
			this.dataGridViewTextBoxColumn6.HeaderText = "MDX Query";
			this.dataGridViewTextBoxColumn6.Name = "Column1";
			this.dataGridViewTextBoxColumn6.ReadOnly = true;
			// 
			// clearGeneratedQueryButton
			// 
			this.clearGeneratedQueryButton.Enabled = false;
			this.clearGeneratedQueryButton.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
			this.clearGeneratedQueryButton.Location = new System.Drawing.Point(452, 485);
			this.clearGeneratedQueryButton.Name = "clearGeneratedQueryButton";
			this.clearGeneratedQueryButton.Size = new System.Drawing.Size(95, 23);
			this.clearGeneratedQueryButton.TabIndex = 4;
			this.clearGeneratedQueryButton.Text = "Clear";
			this.clearGeneratedQueryButton.Click += new System.EventHandler(this.clearGeneratedQueryButton_Click);
			// 
			// numberOfQueriesLabel
			// 
			this.numberOfQueriesLabel.AutoSize = true;
			this.numberOfQueriesLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
			this.numberOfQueriesLabel.Location = new System.Drawing.Point(8, 88);
			this.numberOfQueriesLabel.Name = "numberOfQueriesLabel";
			this.numberOfQueriesLabel.Size = new System.Drawing.Size(110, 15);
			this.numberOfQueriesLabel.TabIndex = 23;
			this.numberOfQueriesLabel.Text = "Number of Queries:";
			// 
			// saveGeneratedQueryButton
			// 
			this.saveGeneratedQueryButton.Enabled = false;
			this.saveGeneratedQueryButton.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
			this.saveGeneratedQueryButton.Location = new System.Drawing.Point(342, 485);
			this.saveGeneratedQueryButton.Name = "saveGeneratedQueryButton";
			this.saveGeneratedQueryButton.Size = new System.Drawing.Size(95, 23);
			this.saveGeneratedQueryButton.TabIndex = 3;
			this.saveGeneratedQueryButton.Text = "Save";
			this.saveGeneratedQueryButton.Click += new System.EventHandler(this.saveGeneratedQueryButton_Click);
			// 
			// numberOfQueriesTextBox
			// 
			this.numberOfQueriesTextBox.BackColor = System.Drawing.SystemColors.ButtonHighlight;
			this.numberOfQueriesTextBox.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.numberOfQueriesTextBox.ImeMode = System.Windows.Forms.ImeMode.Disable;
			this.numberOfQueriesTextBox.Location = new System.Drawing.Point(164, 85);
			this.numberOfQueriesTextBox.MaxLength = 2;
			this.numberOfQueriesTextBox.Name = "numberOfQueriesTextBox";
			this.numberOfQueriesTextBox.Size = new System.Drawing.Size(219, 20);
			this.numberOfQueriesTextBox.TabIndex = 1;
			// 
			// generateQueryButton
			// 
			this.generateQueryButton.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
			this.generateQueryButton.Location = new System.Drawing.Point(418, 81);
			this.generateQueryButton.Name = "generateQueryButton";
			this.generateQueryButton.Size = new System.Drawing.Size(99, 23);
			this.generateQueryButton.TabIndex = 2;
			this.generateQueryButton.Text = "Generate Query";
			this.generateQueryButton.Click += new System.EventHandler(this.generateQueryButton_Click);
			// 
			// cubeNameLabel
			// 
			this.cubeNameLabel.AutoSize = true;
			this.cubeNameLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
			this.cubeNameLabel.Location = new System.Drawing.Point(8, 53);
			this.cubeNameLabel.Name = "cubeNameLabel";
			this.cubeNameLabel.Size = new System.Drawing.Size(72, 15);
			this.cubeNameLabel.TabIndex = 17;
			this.cubeNameLabel.Text = "Cube Name:";
			// 
			// cubeNameTextBox
			// 
			this.cubeNameTextBox.BackColor = System.Drawing.SystemColors.ButtonHighlight;
			this.cubeNameTextBox.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.cubeNameTextBox.ImeMode = System.Windows.Forms.ImeMode.Disable;
			this.cubeNameTextBox.Location = new System.Drawing.Point(164, 49);
			this.cubeNameTextBox.MaxLength = 40;
			this.cubeNameTextBox.Name = "cubeNameTextBox";
			this.cubeNameTextBox.Size = new System.Drawing.Size(219, 20);
			this.cubeNameTextBox.TabIndex = 0;
			// 
			// mdxQeryOpenFileDialog
			// 
			this.mdxQeryOpenFileDialog.Filter = "All files (*.mdx , *.log and *.txt)|*.mdx;*.log;*.txt|MDX Files|*.mdx|Log Files|*" +
				".log|Text Files|*.txt";
			this.mdxQeryOpenFileDialog.Multiselect = true;
			// 
			// ASAddQueryForm
			// 
			this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
			this.ClientSize = new System.Drawing.Size(701, 588);
			this.Controls.Add(this.addQueryTabControl);
			this.Location = new System.Drawing.Point(0, 0);
			this.Name = "ASAddQueryForm";
			this.Text = "Add Query";
			this.Load += new System.EventHandler(this.ASAddQueryForm_Load);
			this.addQueryTabControl.ResumeLayout(false);
			this.importTabPage.ResumeLayout(false);
			this.importTabPage.PerformLayout();
			((System.ComponentModel.ISupportInitialize)(this.ImportQueryDataGridView)).EndInit();
			this.randomQueryTabPage.ResumeLayout(false);
			this.randomQueryTabPage.PerformLayout();
			((System.ComponentModel.ISupportInitialize)(this.randomQueryDataGridView)).EndInit();
			this.ResumeLayout(false);

		}
        #endregion

		#region private void browseButton_Click(object sender, EventArgs e)
		/// <summary>
		/// To select the file(*.mdx | *.txt | *.log) that contain queries
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
        private void browseButton_Click(object sender, EventArgs e)
        {
			ClearImportForm();
			mdxQeryOpenFileDialog.ShowDialog();
			files = mdxQeryOpenFileDialog.FileNames;
			for (int i = 0; i < files.Length; i++)
				selectFileTextBox.Text = files[i];
			if(selectFileTextBox.Text.Trim().Length!=0)	importButton.Enabled = true;
		}
		#endregion

		#region private void importButton_Click(object sender, EventArgs e)

		/// <summary>
		/// Read Queries from the supplied file and list all in DataGridView
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
        private void importButton_Click(object sender, EventArgs e)
        {
			Cursor.Current = Cursors.WaitCursor;
			ImportQueryDataGridView.Rows.Clear();
			string[] stringArrNewQuery;
			XmlDocument xmlDomProjectContent = ProjectFile.GetProjectDetails(projectDetails.ProjectLocation + "\\" + projectDetails.ProjectFileName);
			int totalQueries = int.Parse(xmlDomProjectContent.SelectSingleNode("ProjectFile/TotalQueries").InnerText,CultureInfo.CurrentCulture);
			int rowCount = 0;
			//
			// Fetch each and every selected files from the string array 
			//
			try
			{
				for (int i = 0; i < files.Length; i++)
				{
					//
					//Extract Queries from the each file
					//
					stringArrNewQuery = QueryFile.ImportQuery(files[i]);

					//
					//Retrive queries one by one from the array of string and populate to the DataGridView
					//
					for (int x = 0; x < stringArrNewQuery.Length; x++)
					{
						if (stringArrNewQuery[x].Trim().Length != 0 && !stringArrNewQuery[x].StartsWith("Query:"))
						{
							ImportQueryDataGridView.Rows.Add(1);
							ImportQueryDataGridView.Rows[rowCount].Cells[1].Value = "Query " + (++totalQueries);
							while (xmlDomProjectContent.SelectSingleNode("ProjectFile/MDXQueries/MDXQuery[Name=\"" + ImportQueryDataGridView.Rows[rowCount].Cells[1].Value.ToString().Trim() + "\"]") != null)
							{
								ImportQueryDataGridView.Rows[rowCount].Cells[1].Value = "Query " + (++totalQueries);
							}
							string stringNewQuery = stringArrNewQuery[x].Replace("\r\n", " ");
							stringNewQuery = stringNewQuery.Replace("\t", " ");
							ImportQueryDataGridView.Rows[rowCount].Cells[2].Value = stringNewQuery;
							ImportQueryDataGridView.Rows[rowCount].Cells[2].ToolTipText = stringArrNewQuery[x];
							ImportQueryDataGridView.Rows[rowCount].Cells[0].Value = ++rowCount;
						}
					}
				}
				if (rowCount != 0)
				{
					importButton.Enabled = false;
					messageLabel.Visible = true;
					saveButton.Enabled = true;
					clearButton.Enabled = true;
				}
			}
			catch (Exception ex)
			{
				EventLog.WriteEntry("AS Upgrade Verification Tool", ex.ToString(), EventLogEntryType.Error);
			}
			Cursor.Current = Cursors.Default;
		}
		#endregion

		#region private void saveButton_Click(object sender, EventArgs e)

		/// <summary>
		/// Save all queries listed in DatagridView to the current project file
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
        private void saveButton_Click(object sender, EventArgs e)
        {
			try
			{
				bool duplicationStatus = CheckForDuplicateQueryName(ImportQueryDataGridView);
				
				if (duplicationStatus == true)
				{
					throw new CustomAppException("customException27");
				}
				else
				{
					//
					//Save all the imported queries to project file
					//
					SaveQueries(ImportQueryDataGridView);
					ClearImportForm();
				}
			}
			catch (CustomAppException ex)
			{
				ExceptionManager.Publish(ex);
			}
		}
		#endregion

		#region private void clearButton_Click(object sender, EventArgs e)
		private void clearButton_Click(object sender, EventArgs e)
		{
			DialogResult result = MessageBox.Show("Do you want to clear all the imported queries?", "AS Upgrade Verification Tool", MessageBoxButtons.OKCancel,MessageBoxIcon.Question,MessageBoxDefaultButton.Button1,MessageBoxOptions.DefaultDesktopOnly);
			if(result==DialogResult.OK)	ClearImportForm();
		}
		#endregion

		#region private void ClearImportForm()
		/// <summary>
		/// Method to clear the all contents in the import form
		/// </summary>
		private void ClearImportForm()
		{
			ImportQueryDataGridView.Rows.Clear();
			selectFileTextBox.Text = "";
			messageLabel.Visible = false;
			saveButton.Enabled = false;
			clearButton.Enabled = false;
			importButton.Enabled = false;
		}
		#endregion

		#region private void ClearRandomForm()
		/// <summary>
		/// Method to clear the all contents in the Random cell form
		/// </summary>
		private void ClearRandomForm()
		{
			randomQueryDataGridView.Rows.Clear();
			cubeNameTextBox.Text = "";
		    numberOfQueriesTextBox.Text = "";
			message1Label.Visible = false;
			saveGeneratedQueryButton.Enabled = false;
			clearGeneratedQueryButton.Enabled = false;
			generateQueryButton.Enabled = true;
		}
		#endregion

		#region private void generateQueryButton_Click(object sender, EventArgs e)
		/// <summary>
		/// Iterate through selected files one by one and populate all the quries to DataGridView 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void generateQueryButton_Click(object sender, EventArgs e)
		{
			randomQueryDataGridView.Rows.Clear();
			Cursor.Current = Cursors.WaitCursor;
			try
			{
				int queryCount = 0;
				if (cubeNameTextBox.Text.Trim().Length != 0)
				{
					if (numberOfQueriesTextBox.Text.Trim().Length == 0)
					{
						numberOfQueriesTextBox.Focus();
						throw new CustomAppException("customException11");
					}
					if (!IsInteger(numberOfQueriesTextBox.Text.Trim()))
					{
						numberOfQueriesTextBox.Text = "";
						numberOfQueriesTextBox.Focus();
						throw new CustomAppException("customException18");
					}
					else
					{
						queryCount = int.Parse(numberOfQueriesTextBox.Text.Trim(), CultureInfo.CurrentCulture);
					}
					AnalysisService analysisService = new AnalysisService();
					//
					//Gnerate Query form the supplied  cube  
					//
					string[] stringArrNewQuery = analysisService.GenerateQuery(cubeNameTextBox.Text.Trim().ToUpper(CultureInfo.CurrentCulture), queryCount, projectDetails);
					//
					//Retrieve project contents from the project file
					//
					XmlDocument xmlDomProjectContent = ProjectFile.GetProjectDetails(projectDetails.ProjectLocation + "\\" + projectDetails.ProjectFileName);

					int totalQueries = int.Parse(xmlDomProjectContent.SelectSingleNode("ProjectFile/TotalQueries").InnerText, CultureInfo.CurrentCulture);
					DataGridView generatedqueries = randomQueryDataGridView;

					//
					//Populate generated queries to DataGridView
					//
					for (int i = 0, x = 0; x < stringArrNewQuery.Length; x++)
					{
						if (stringArrNewQuery[x].Trim().Length != 0 && !stringArrNewQuery[x].StartsWith("Query:") && stringArrNewQuery[0].Trim() != "INVALID")
						{
							generatedqueries.Rows.Add(1);
							generatedqueries.Rows[i].Cells[1].Value = "Query " + (++totalQueries);
							while (xmlDomProjectContent.SelectSingleNode("ProjectFile/MDXQueries/MDXQuery[Name=\"" + generatedqueries.Rows[i].Cells[1].Value.ToString().Trim() + "\"]") != null)
							{
								generatedqueries.Rows[i].Cells[1].Value = "Query " + (++totalQueries);
							}
							string stringNewQuery = stringArrNewQuery[x].Replace("\r\n", " ");
							stringNewQuery = stringNewQuery.Replace("\t", " ");
							generatedqueries.Rows[i].Cells[2].Value = stringNewQuery;
							generatedqueries.Rows[i].Cells[2].ToolTipText = stringArrNewQuery[x];
							generatedqueries.Rows[i].Cells[0].Value = ++i;
						}
					}
					if (generatedqueries.Rows.Count > 0)
					{
						message1Label.Visible = true;
						saveGeneratedQueryButton.Enabled = true;
						clearGeneratedQueryButton.Enabled = true;
						generateQueryButton.Enabled = false;
					}
					if ((queryCount > generatedqueries.Rows.Count) && stringArrNewQuery[1].Trim().Length != 0)
					{
						int existingQueryCount = generatedqueries.Rows.Count;
						CustomPublisherException ex = new CustomPublisherException(existingQueryCount.ToString(CultureInfo.CurrentCulture));
						throw new CustomAppException("customException22", ex);
					}

				}
				else
				{
					cubeNameTextBox.Focus();
					throw new CustomAppException("customException10");
				}
			}
			catch (CustomAppException ex)
			{
				ExceptionManager.Publish(ex);
			}
			catch (Exception ex)
			{
				EventLog.WriteEntry("AS Upgrade Verification Tool", ex.ToString(), EventLogEntryType.Error); 
			}
			Cursor.Current = Cursors.Default;
		}
		#endregion

		#region private void saveGeneratedQueryButton_Click(object sender, EventArgs e)
		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void saveGeneratedQueryButton_Click(object sender, EventArgs e)
		{
			try
			{
				bool duplicationStatus = CheckForDuplicateQueryName(randomQueryDataGridView);

				if (duplicationStatus == true)
				{
					throw new CustomAppException("customException27");
				}
				else
				{
					//
					//Save generated queries to project file
					//
					SaveQueries(randomQueryDataGridView);
					ClearRandomForm();
				}
			}
			catch (CustomAppException ex)
			{
				ExceptionManager.Publish(ex);
			}
			catch (Exception ex)
			{
				EventLog.WriteEntry("AS Upgrade Verification Tool", ex.ToString(), EventLogEntryType.Error); 
			}
		}
		#endregion

		#region private static bool IsInteger(string numeric)
		/// <summary>
		/// Check wether the whether suplied string is an integer or not
		/// </summary>
		/// <param name="numeric"></param>
		/// <returns></returns>
		private static bool IsInteger(string numeric)
		 {
			char[] chars = numeric.ToCharArray();
			for(int i = 0;i< chars.Length;i++)
				if(!Char.IsDigit(chars[i])) return false;
			return true;
		}
		#endregion

		// TODOS in this one...
		#region private void SaveQueries(DataGridView queryDataGridView)
		/// <summary>
		/// Save all queries in the datagrid to project file
		/// </summary>
		/// <param name="queryDataGridView">DataGridView</param>
		private void SaveQueries(DataGridView queryDataGridView)
		{
			XmlDocument xmlDomProjectContent = ProjectFile.GetProjectDetails(projectDetails.ProjectLocation + "\\" + projectDetails.ProjectFileName);
			StringBuilder strNewQueries = new StringBuilder();
			StringBuilder strExistingQueries = new StringBuilder();
			int totalQueries = int.Parse(xmlDomProjectContent.SelectSingleNode("ProjectFile/TotalQueries").InnerText,CultureInfo.CurrentCulture);
			int count = queryDataGridView.RowCount;
			int i, duplicateQueries = 0;

			try
			{
				for (i = 0; i < count; i++)
				{
					bool duplicateQueryStatus = false;
					//
					//Check whether  duplicate query exist in the list of imported queries
					//
					for (int j = i + 1; j < count; j++)
					{
						if (queryDataGridView.Rows[i].Cells[2].Value.ToString().Trim() == queryDataGridView.Rows[j].Cells[2].Value.ToString().Trim())
						{
							duplicateQueryStatus = true;
							break;
						}
					}
					//
					//Check whether query already exist in the project file or not
					//
					string query = queryDataGridView.Rows[i].Cells[2].Value.ToString().Trim().Replace("&", "amp;");
					query = query.Replace("<", "lt;");
					query = query.Replace(">", "gt;");
					query = query.Replace("\"", "quote;");
					if (xmlDomProjectContent.SelectSingleNode("ProjectFile/MDXQueries/MDXQuery[QueryAS2K=\"" + query + "\"]") == null && !duplicateQueryStatus)
					{
						try
						{
							string queryName = queryDataGridView.Rows[i].Cells[1].Value.ToString().Trim().Replace("\"", "");
							queryName = queryName.Replace("\\", "");
							queryName = queryName.Replace("<", "");
							queryName = queryName.Replace(">", "");
							queryName = queryName.Replace("&", "");
							string dbName = (projectDetails.DatabaseName != null) ? projectDetails.DatabaseName.Replace("&", "amp;") : "";

							strNewQueries.Append("<MDXQuery><ID>" + Guid.NewGuid() + "</ID><Name>" + queryName
										+ "</Name><DatabaseName>" + dbName + "</DatabaseName><CubeName>" + cubeNameTextBox.Text.Trim().Replace("&", "amp;") + "</CubeName><NumberOfCells></NumberOfCells>"
										+ "<QueryAS2K><![CDATA[" + query + "]]></QueryAS2K><QueryAS2K5><![CDATA[" + query + "]]></QueryAS2K5>"
										+ "<AS2KResult></AS2KResult><AS2K5Result></AS2K5Result><AS2KQueryStatus>Query not Executed</AS2KQueryStatus>"
										+ "<AS2K5QueryStatus>Query not Executed</AS2K5QueryStatus><AS2KExecDuration></AS2KExecDuration>"
										+ "<AS2K5ExecDuration></AS2K5ExecDuration><ComparisonResult>Not Compared</ComparisonResult></MDXQuery>");
							totalQueries++;
						}
						catch (Exception ex)
						{
							EventLog.WriteEntry("AS Upgrade Verification Tool", ex.ToString(), EventLogEntryType.Error);
						}
					}
					else
					{
						duplicateQueries++;
						strExistingQueries.Append(Environment.NewLine + duplicateQueries.ToString(CultureInfo.CurrentCulture) + ". " + queryDataGridView.Rows[i].Cells[1].Value.ToString());
					}

				}


				xmlDomProjectContent.SelectSingleNode("ProjectFile/TotalQueries").InnerText = totalQueries.ToString(CultureInfo.CurrentCulture);
				XmlNode xmlNewNodes = xmlDomProjectContent.CreateElement("MDXQueries");
				xmlNewNodes.InnerXml = strNewQueries.ToString();
				foreach (XmlNode oNode in xmlNewNodes.SelectNodes(".//MDXQuery"))
				{
					xmlDomProjectContent.SelectSingleNode("ProjectFile/MDXQueries").AppendChild(oNode);
				}
				xmlDomProjectContent.SelectSingleNode("ProjectFile/LastModifiedDateTime").InnerText = DateTime.Now.ToString();
				ProjectFile.SetProjectDetails(xmlDomProjectContent, projectDetails.ProjectLocation + "\\" + projectDetails.ProjectFileName);
				if (totalQueries > 0)
				{
					QueriesAdded();
				}
				if (duplicateQueries != 0)
				{
					CustomPublisherException obj = new CustomPublisherException(strExistingQueries.ToString());
					throw new CustomAppException("queryExist", obj);
				}
				else
				{
					throw new CustomAppException("customException26");
				}
			}
			catch (CustomAppException ex)
			{
				ExceptionManager.Publish(ex);
			}
			catch (Exception ex)
			{
				EventLog.WriteEntry("AS Upgrade Verification Tool", ex.ToString(), EventLogEntryType.Error);
			}

		}
		#endregion

		#region private void clearGeneratedQueryButton_Click(object sender, EventArgs e)
		private void clearGeneratedQueryButton_Click(object sender, EventArgs e)
		{
			DialogResult result = MessageBox.Show("Do you want to clear all the queries?", "AS Upgrade Verification Tool", MessageBoxButtons.OKCancel,MessageBoxIcon.Question,MessageBoxDefaultButton.Button1,MessageBoxOptions.DefaultDesktopOnly);
			if (result == DialogResult.OK) ClearRandomForm();
		}
		#endregion

		#region private void closeButton_Click(object sender, EventArgs e)
		private void closeButton_Click(object sender, EventArgs e)
		{
			this.Close();
		}
		#endregion

		#region private void close1Button_Click(object sender, EventArgs e)
		private void close1Button_Click(object sender, EventArgs e)
		{
			this.Close();
		}
		#endregion

		#region private void ASAddQueryForm_Load(object sender, EventArgs e)
		private void ASAddQueryForm_Load(object sender, EventArgs e)
		{
			cubeNameTextBox.Focus();
		}
		#endregion

		#region private bool CheckForDuplicateQueryName(DataGridView dataGridView)
		/// <summary>
		/// Check whether the query name already exist in the project file or not
		/// </summary>
		/// <param name="dataGridView"></param>
		/// <returns></returns>
		private bool CheckForDuplicateQueryName(DataGridView dataGridView)
		{
			bool status=false;
			int count = dataGridView.RowCount;
			XmlDocument xmlDomProjectContent = ProjectFile.GetProjectDetails(projectDetails.ProjectLocation + "\\" + projectDetails.ProjectFileName);
			string[] stringArrQueryName = new string[count];
			for (int i = 0; i < count; i++)
			{
				stringArrQueryName[i] = dataGridView.Rows[i].Cells[1].Value.ToString().Trim();
				stringArrQueryName[i] = stringArrQueryName[i].Replace("\"", "");
				stringArrQueryName[i] = stringArrQueryName[i].Replace("\\", "");
				stringArrQueryName[i] = stringArrQueryName[i].Replace("<", "");
				stringArrQueryName[i] = stringArrQueryName[i].Replace(">", "");
				stringArrQueryName[i] = stringArrQueryName[i].Replace("&", "");
				if (xmlDomProjectContent.SelectSingleNode("ProjectFile/MDXQueries/MDXQuery[Name=\"" + stringArrQueryName[i].Trim() + "\"]") != null)
				{
					dataGridView.Rows[i].Cells[1].Style.BackColor = Color.Blue;
					dataGridView.Rows[i].Cells[1].Style.ForeColor = Color.White;
					dataGridView.Rows[i].Cells[1].Selected = true;
					status = true;
				}
				else
				{
					dataGridView.Rows[i].Cells[1].Style.BackColor = Color.White;
					dataGridView.Rows[i].Cells[1].Style.ForeColor = Color.Black;
					dataGridView.Rows[i].Cells[1].Selected = false;
				}
			}
			for (int i = 0; i < count; i++)
			{
				for (int j = i + 1; j < count; j++)
				{
					if(stringArrQueryName[i]==stringArrQueryName[j])
					{
						dataGridView.Rows[j].Cells[1].Style.BackColor = Color.Blue;
						dataGridView.Rows[j].Cells[1].Style.ForeColor = Color.White;
						dataGridView.Rows[j].Cells[1].Selected = true;
						status = true;
					}
				}
			}

			return status;
		}
		#endregion

		#region private void ImportQueryDataGridView_UserDeletedRow(object sender, DataGridViewRowEventArgs e)
		private void ImportQueryDataGridView_UserDeletedRow(object sender, DataGridViewRowEventArgs e)
		{
			if (ImportQueryDataGridView.Rows.Count == 0)
			{
				ClearImportForm();
			}

		}
		#endregion

		#region private void randomQueryDataGridView_UserDeletedRow(object sender, DataGridViewRowEventArgs e)
		private void randomQueryDataGridView_UserDeletedRow(object sender, DataGridViewRowEventArgs e)
		{
			if (randomQueryDataGridView.Rows.Count == 0)
			{
				ClearRandomForm();
			}
		}
		#endregion

	}
}

